﻿
// ~ Package Declaration
// ==================================================

package com.violet.common;

import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibatis.common.logging.Log;
import com.ibatis.common.logging.LogFactory;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

// ~ Comments
// ==================================================

/**
 * DAO�@����.
 * 
 * <pre>
 * ��v����G
 * 2011/8/16 SteveXing
 * 	�s���ɮ�
 * </pre>
 * 
 * @author 
 * <pre>
 * SD
 * 	SteveXing
 * PG
 *
 * UT
 *
 * MA
 * </pre>
 * @version $Rev$
 *
 * <p/> $Id$
 *
 */
public class CommonDao {

	// ~ Static Fields
	// ==================================================
	private static SqlMapClient sqlMap;
	
	//��l��sqlMap�AŪ��sql-map-config.xml
	static {
		
		try {
			
			String resource = "sql-map-config.xml";
			
			Reader reader = Resources.getResourceAsReader (resource);
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		} 
		catch (Exception e) {
			
			sqlMap = null;
			
			try {
				
				throw new Exception ("Error initializing SqlMapConfig class. Cause: " + e);
			} 
			catch (Exception e1) {
				
				e1.printStackTrace();
			}
		}
	}
	
	// ~ Fields
	// ==================================================
	protected final Log logger = LogFactory.getLog(getClass());
	
	// ~ Constructors
	// ==================================================

	// ~ Methods
	// ==================================================
	
	/**
	 * <pre>
	 * 2011/8/16 SteveXing
	 * 	����queryForList���d�ߵ��G.
	 * </pre>
	 * 
	 * @param statementName sqlId
	 * @param param
	 * @return List
	 */
	public List queryForList(String statementName, Object param) {

		if (logger.isDebugEnabled()) {
			
			logger.debug("statement: " + statementName);
		}
		
		List lstQueryResult = new ArrayList();
		
		try {
			
			lstQueryResult = sqlMap.queryForList(statementName, param);
		}
		catch (SQLException e) {

			e.printStackTrace();
		}
		
		return lstQueryResult;
	}
	
	/**
	 * <pre>
	 * 2011/8/16 SteveXing
	 * 	����queryForList���d�ߵ��G.
	 * </pre>
	 * 
	 * @param statementName sqlId
	 * @return List
	 */
	public List queryForList(String statementName) {
		
		if (logger.isDebugEnabled()) {
			
			logger.debug("statement: " + statementName);
		}
		
		List lstQueryResult = new ArrayList();
		
		try {
			
			lstQueryResult = sqlMap.queryForList(statementName);
		}
		catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return lstQueryResult;
	}
	
	/**
	 * <pre>
	 * 2011/8/16 SteveXing
	 * 	����R���ާ@.
	 * </pre>
	 * 
	 * @param statementName sqlId
	 * @return int �R����Ƶ���
	 */
	public int delete(String statementName) {
		
		if (logger.isDebugEnabled()) {
			
			logger.debug("statement: " + statementName);
		}

		int _count = 0;
		
		try {
			
			_count = sqlMap.delete(statementName);
		}
		catch (SQLException e) {

			e.printStackTrace();
		}

		return _count;
	}

	/**
	 * <pre>
	 * 2011/8/16 SteveXing
	 * 	����R���ާ@.
	 * </pre>
	 * 
	 * @param statementName sqlId
	 * @param param
	 * @return int �R����Ƶ���
	 */
	public int delete(String statementName, Object param) {

		if (logger.isDebugEnabled()) {
			
			logger.debug("statement: " + statementName);
		}

		int _count = 0;
		
		try {
			
			_count = sqlMap.delete(statementName, param);
		}
		catch (SQLException e) {

			e.printStackTrace();
		}

		return _count;

	}

	/**
	 * <pre>
	 * 2011/8/16 SteveXing
	 * 	����s�W�ާ@.
	 * </pre>
	 * 
	 * @param statementName sqlId
	 * @param param
	 * @return boolean �s�W���\�Gtrue�F�s�W���ѡGfasle
	 */
	public boolean insert(String statementName, Object param) {
		
		boolean blnInsertResult = false;
		
		if (logger.isDebugEnabled()) {
			
			logger.debug("statement: " + statementName);
		}

		try {
			
			sqlMap.insert(statementName, param);
			
			blnInsertResult = true;
		}
		catch (SQLException e) {

			e.printStackTrace();
		}
		
		return blnInsertResult;
	}
	
	/**
	 * <pre>
	 * 2011/8/16 SteveXing
	 * 	����ק�ާ@.
	 * </pre>
	 * 
	 * @param statementName sqlId
	 * @param param
	 * @return int �ק��Ƶ���
	 */
	public int update(String statementName, Object param) {

		if (logger.isDebugEnabled()) { 
			
			logger.debug("statement:" + statementName);
		}

		int _count = 0;
		
		try {
			
			_count = sqlMap.update(statementName, param);
		}
		catch (SQLException e) {

			e.printStackTrace();
		}
		
		return _count;
	}
	
	/**
	 * <pre>
	 * 2011/8/16 SteveXing
	 * 	����queryForMap���d�ߵ��G.
	 * </pre>
	 * 
	 * @param statementName sqlId
	 * @param param
	 * @param keyProperty
	 * @return Map
	 */
	public Map queryForMap(String statementName, Object param, String keyProperty) {

		if (logger.isDebugEnabled()) {
			
			logger.debug("statement:" + statementName + " keyProperty=" + keyProperty);
		}

		Map map = new HashMap();
		
		try {
			
			map = sqlMap.queryForMap(statementName, param, keyProperty);
		}
		catch (SQLException e) {

			e.printStackTrace();
		}
		
		return map;
	}

	/**
	 * <pre>
	 * 2011/8/16 SteveXing
	 * 	����queryForMap���d�ߵ��G.
	 * </pre>
	 * 
	 * @param statementName sqlId
	 * @param param
	 * @param keyProperty
	 * @param valueProperty
	 * @return Map
	 */
	public Map queryForMap(String statementName, Object param, String keyProperty,
			String valueProperty) {

		if (logger.isDebugEnabled()) {
			
			logger.debug("statement:" + statementName + " keyProperty=" + keyProperty);
		}

		Map map = new HashMap();
		
		try {
			
			map = sqlMap.queryForMap(statementName, param, keyProperty, valueProperty);
		}
		catch (SQLException e) {

			e.printStackTrace();
		}
		
		return map;
	}
	
	/**
	 * <pre>
	 * 2011/8/16 SteveXing
	 * 	����queryForObject���d�ߵ��G.
	 * </pre>
	 * 
	 * @param statementName sqlId
	 * @return Object
	 */
	public Object queryForObject(String statementName) {

		return queryForObject(statementName, null);
	}

	/**
	 * <pre>
	 * 2011/8/16 SteveXing
	 * 	����queryForMap���d�ߵ��G.
	 * </pre>
	 * 
	 * @param statementName sqlId
	 * @param param
	 * @param keyProperty
	 * @param valueProperty
	 * @return Map
	 */
	public Object queryForObject(String statementName, Object param) {

		if (logger.isDebugEnabled()) {
			
			logger.debug("statement:" + statementName);
		}

		Object object = null;
		
		try {
			
			object = sqlMap.queryForObject(statementName, param);
		}
		catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return object;
	}
}
